package io.renren.modules.bike.service.impl;

import io.renren.modules.bike.entity.BikeEntity;
import io.renren.modules.bike.entity.NoticeEntity;
import io.renren.modules.bike.service.BikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.bike.dao.OrdersDao;
import io.renren.modules.bike.entity.OrdersEntity;
import io.renren.modules.bike.service.OrdersService;

import static io.renren.common.utils.ShiroUtils.getUserId;


@Service("ordersService")
public class OrdersServiceImpl extends ServiceImpl<OrdersDao, OrdersEntity> implements OrdersService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String) params.get("key");
        IPage<OrdersEntity> page = null;
        if (key != null) {
            page = this.page(
                    new Query<OrdersEntity>().getPage(params),
                    new QueryWrapper<OrdersEntity>().orderByDesc("oid").like("o_bid", key)
                            .or().like("o_state", key)
            );
        } else {
            page = this.page(
                    new Query<OrdersEntity>().getPage(params),
                    new QueryWrapper<OrdersEntity>().orderByDesc("oid")
            );
        }


        return new PageUtils(page);
    }

    @Autowired
    BikeService bikeService;

    @Override
    public PageUtils queryPageWeiXiu(Map<String, Object> params) {
        IPage<BikeEntity> page =   bikeService.page(
                new Query<BikeEntity>().getPage(params),
                new QueryWrapper<BikeEntity>().like("b_state", "归还,单车需维修")
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageUser(Map<String, Object> params) {
        Long userId = getUserId();
        IPage<OrdersEntity> page = this.page(
                new Query<OrdersEntity>().getPage(params),
                new QueryWrapper<OrdersEntity>().eq("o_sno", userId).orderByDesc("oid")
        );

        return new PageUtils(page);
    }
}
